<?php
/**
 * Created by PhpStorm.
 * User: superxing-cto
 * Date: 17/10/23
 * Time: 12:05
 */

namespace App\Models;
use App\Models\User;
use App\Models\ContributionValueLog;
use App\Http\Helper\UrlSchemaHelper;
/**
 * Class Follow
 * @property integer user_id 用户id
 * @property integer type_id 类型id
 * @property string type 类型
 * @property boolean is_push 是否接收推送
 * @property boolean is_mutual 是否互相关注
 */
class Fan extends ApplicationModel
{
    protected $fillable = [
        'user_id', //用户id
        'type_id', //类型id
        'type', //类型
        'is_push', //是否接收推送
        'is_mutual', //是否互相关注
        'value', //贡献值
        'rank',
        'user_value',
        'work_status',
        'get_status',
        'content',
        'work_start_time',
        'work_end_time',
        'coin',
        'work_value',
        'type_social_status'
    ];

    const STATUS_NONE = 0;//
    const STATUS_WORKING = 1;//打工中
    const STATUS_WORKED = 2;//已结束

    const STATUS_NO_GET = 0;//未领取
    const STATUS_HAVE_GET = 1;//已领

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function followUser()
    {
        return $this->belongsTo(User::class, 'type_id');
    }

    public function profit()
    {
        $rank = $this->user_rank();
        $user = User::find($this->type_id);
        $star_level = $user->star_level;
        $profit = 0;
        if( $star_level=='0' )
        {
           if( $rank < 12 )
           {
               $profit = 21 - $rank;
           }
           else
           {
               $profit = 10;
           }


        }

        if( $star_level=='0.5' )
        {

            if( $rank>0 &&  $rank<4 )
            {
                  $profit = 26 - $rank;
            }
            else if( $rank > 3 && $rank < 12 )
            {

                $profit = 25 - $rank;
            }
            else
            {
                $profit = 13;
            }


        }

        if( $star_level=='1'  )
        {
            if($rank>0 && $rank<3)
            {
                $profit = 31 - $rank;
            }
            else if( $rank > 2 && $rank < 5 )
            {
                 $profit = 30 - $rank;
            }
            else if( $rank> 4 && $rank < 7 )
            {
                $profit = 29 - $rank;
            }
            else if( $rank>6 && $rank < 9 )
            {
                $profit =  28 - $rank;
            } else if( $rank>8 && $rank<11){
                $profit = 27 - $rank;
            } else {
                $profit = 15;
            }

        }

        if(  $star_level=='1.5' )
        {
            switch($rank)
            {
                case 1:
                    $profit = 35;
                    break;
                case 2:
                    $profit = 33;
                    break;
                case 3:
                    $profit = 32;
                    break;
                case 4:
                    $profit = 30;
                    break;
                case 5:
                    $profit = 28;
                    break;
                case 6:
                    $profit = 26;
                    break;
                case 7:
                    $profit = 25;
                    break;
                case 8:
                    $profit = 23;
                    break;
                case 9:
                    $profit = 21;
                    break;
                case 10:
                    $profit = 19;
                    break;
                default:
                    $profit = 18;
            }

        }

        if( $star_level=='2'  )
        {

            switch($rank)
            {
                case 1:
                    $profit = 40;
                    break;
                case 2:
                    $profit = 38;
                    break;
                case 3:
                    $profit = 36;
                    break;
                case 4:
                    $profit = 34;
                    break;
                case 5:
                    $profit = 32;
                    break;
                case 6:
                    $profit = 30;
                    break;
                case 7:
                    $profit = 28;
                    break;
                case 8:
                    $profit = 26;
                    break;
                case 9:
                    $profit = 24;
                    break;
                case 10:
                    $profit = 22;
                    break;
                default:
                    $profit = 20;
            }
        }

        if(  $star_level=='2.5' )
        {
            switch($rank)
            {
                case 1:
                    $profit = 45;
                    break;
                case 2:
                    $profit = 43;
                    break;
                case 3:
                    $profit = 41;
                    break;
                case 4:
                    $profit = 38;
                    break;
                case 5:
                    $profit = 36;
                    break;
                case 6:
                    $profit = 34;
                    break;
                case 7:
                    $profit = 32;
                    break;
                case 8:
                    $profit = 29;
                    break;
                case 9:
                    $profit = 27;
                    break;
                case 10:
                    $profit = 25;
                    break;
                default:
                    $profit = 23;
            }

        }
        if( $star_level=='3'  )
        {
            switch($rank)
            {
                case 1:
                    $profit = 50;
                    break;
                case 2:
                    $profit = 48;
                    break;
                case 3:
                    $profit = 45;
                    break;
                case 4:
                    $profit = 43;
                    break;
                case 5:
                    $profit = 40;
                    break;
                case 6:
                    $profit = 38;
                    break;
                case 7:
                    $profit = 35;
                    break;
                case 8:
                    $profit = 33;
                    break;
                case 9:
                    $profit = 30;
                    break;
                case 10:
                    $profit = 28;
                    break;
                default:
                    $profit = 25;
            }

        }

        if(  $star_level=='3.5' )
        {
            switch($rank)
            {
                case 1:
                    $profit = 55;
                    break;
                case 2:
                    $profit = 52;
                    break;
                case 3:
                    $profit = 50;
                    break;
                case 4:
                    $profit = 47;
                    break;
                case 5:
                    $profit = 44;
                    break;
                case 6:
                    $profit = 41;
                    break;
                case 7:
                    $profit = 39;
                    break;
                case 8:
                    $profit = 36;
                    break;
                case 9:
                    $profit = 33;
                    break;
                case 10:
                    $profit = 30;
                    break;
                default:
                    $profit = 28;
            }
        }
        if( $star_level=='4'  )
        {
           switch($rank)
           {
               case 1:
                   $profit = 60;
                   break;
               case 2:
                   $profit = 57;
                   break;
               case 3:
                   $profit = 54;
                   break;
               case 4:
                   $profit = 51;
                   break;
               case 5:
                   $profit = 48;
                   break;
               case 6:
                   $profit = 45;
                   break;
               case 7:
                   $profit = 42;
                   break;
               case 8:
                   $profit = 39;
                   break;
               case 9:
                   $profit = 36;
                   break;
               case 10:
                   $profit = 33;
                   break;
               default:
                   $profit = 30;
           }
        }
        if(  $star_level=='4.5' )
        {
            switch($rank)
            {
                case 1:
                    $profit = 65;
                    break;
                case 2:
                    $profit = 62;
                    break;
                case 3:
                    $profit = 59;
                    break;
                case 4:
                    $profit = 55;
                    break;
                case 5:
                    $profit = 52;
                    break;
                case 6:
                    $profit = 49;
                    break;
                case 7:
                    $profit = 46;
                    break;
                case 8:
                    $profit = 42;
                    break;
                case 9:
                    $profit = 39;
                    break;
                case 10:
                    $profit = 36;
                    break;
                default:
                    $profit = 33;
            }

        }
        if( $star_level=='5' )
        {
            switch($rank)
            {
                case 1:
                    $profit = 70;
                    break;
                case 2:
                    $profit = 67;
                    break;
                case 3:
                    $profit = 63;
                    break;
                case 4:
                    $profit = 60;
                    break;
                case 5:
                    $profit = 56;
                    break;
                case 6:
                    $profit = 53;
                    break;
                case 7:
                    $profit = 49;
                    break;
                case 8:
                    $profit = 46;
                    break;
                case 9:
                    $profit = 42;
                    break;
                case 10:
                    $profit = 39;
                    break;
                default:
                    $profit = 35;
            }
        }
        return $profit;
    }

    public function user_rank()
    {
        $rank = 1;
        $user = User::find($this->type_id);
        $user_id = $this->user_id;
        if($user)
        {
            $fans = $user->myFanUsers()->orderBy('value','desc')->get();
            if( count($fans)>0 )
            {
                foreach($fans as $k=>$fan)
                {
                    if($user_id == $fan->user_id)
                    {
                        $rank = $k+1;
                        break;
                    }
                }
            }

        }

       return $rank;

    }


    public function send_work_social_status_msg($content,$url = null)
    {
        //发送推送
        $message = new Message();
        $message->user_id = $this->type_id;
        $message->content = $content;
        $message->icon = Message::ICON;
        $message->url_schema = $url;
        $message->type_id = $this->id;
        $message->type = 'socialstatus';
        $message->save();

        //$message->pushMsg($this->type_id, $content);

    }

    public function send_new_fans_msg($content,$url = null)
    {
        //发送推送
        $message = new Message();
        $message->user_id = $this->type_id;
        $message->content = $content;
        $message->icon = Message::SOCIA_STATUS_UP_ICON;
        $message->url_schema = $url;
        $message->type_id = $this->id;
        $message->type = 'socialstatus';
        $message->save();

        //$message->pushMsg($this->type_id, $content);

    }

    public function send_msg($content, $url = null)
    {
        //发送推送
        $message = new Message();
        $message->user_id = $this->type_id;
        $message->content = $content;
        $message->icon = Message::ICON;
        $message->url_schema = $url;
        $message->type_id = $this->id;
        $message->save();

        $message->pushMsg($this->type_id, $content);
    }

    //打工消息
    public function send_work_msg($content,$url= null )
    {
        //发送推送

        $type = 'idol';
        $url = UrlSchemaHelper::get_url_schema("idol");
        $params =  "{'url':'" . UrlSchemaHelper::get_url_schema("idol")."'}";
        $message = new Message();
        $message->user_id = $this->user_id;
        $message->content = $content;
        $message->icon = Message::IDOL_WORK_ICON;
        $message->url_schema = $url;
        $message->type_id = $this->id;
        $message->type = $type;
        $message->save();
        $message->pushMsg($this->type_id, $content, $params);


    }

    //消息
    public function idol_quit_msg($content,$url= null )
    {
        //发送推送
        $type = 'idol';
        $url = UrlSchemaHelper::get_url_schema("idol");
        $params =  "{'url':'" . UrlSchemaHelper::get_url_schema("idol")."'}";
        $message = new Message();
        $message->user_id = $this->user_id;
        $message->content = $content;
        $message->icon = Message::IDOL_QUIT_ICON;
        $message->url_schema = $url;
        $message->type_id = $this->id;
        $message->type = $type;
        $message->save();
        $message->pushMsg($this->type_id, $content, $params);

    }



    public function add_contribution_value($type=ContributionValueLog::TYPE_SHARE)
    {


            $user = User::find($this->user_id);
            $idol = User::find($this->type_id);

            if($user && $idol)
            {

                $value = 10;
                $detail = '';
                if( $type==ContributionValueLog::TYPE_SHARE )
                {
                    $detail =  $user->name." "."分享"."偶像"." ".$idol->name." ".""." ".""." "."贡献值".$value;
                }
                else if( $type == ContributionValueLog::TYPE_WORK )
                {
                    $detail =  $user->name." "."为"."偶像"." ".$idol->name." "."打工"." ".""." "."贡献值".$value;

                }
                else if( $type == ContributionValueLog::TYPE_ITEM )
                {
                    $detail =  $user->name." "."送"."偶像"." ".$idol->name." "."道具"." ".""." "."贡献值".$value;
                }


                $contribution_value_log = new ContributionValueLog();
                $contribution_value_log->user_id  = $this->user_id;
                $contribution_value_log->to_user_id  = $this->type_id;


                $contribution_value_log->value  = $value;
                $contribution_value_log->detail  = $detail;
                $contribution_value_log->type  = $type;
                $contribution_value_log->source = 'app';
                $contribution_value_log->save();

               // $this->value = $this->value + $value;
                //$this->user_value = $this->user_value + $value;
               // $this->save();
            }
    }


    public function work_flag()
    {

        $work_flag = -1;
        $user_id = $this->user_id;
         $count = Fan::where('user_id',$user_id)->where('work_status',$this::STATUS_WORKING)->count();
        if( $count == 0 )
        {
            $work_flag = 1;
        }
       return $work_flag;

    }



}
